<template>
  <div>
    <el-dialog title="绑定Token" :visible.sync="show" width="500px">
      <el-form :model="tokenForm" ref="tokenForm" label-width="100px">
        <el-form-item label="GitHub">
          <el-tag>{{ configs.githubUsername }}</el-tag>
        </el-form-item>
        <el-form-item
          prop="token"
          label="Token"
          :rules="[
            { required: true, message: '请输入accessToken', trigger: 'blur' },
          ]"
        >
          <el-input
            v-model="tokenForm.token"
            placeholder="请保管好Token"
          ></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<static-query>
query {
  metadata {
    siteName
  },
  configs:allConfig{
    totalCount
    edges{
      node{
        id
        githubUsername
        blogTitle
        blogDescribe
        backgroundColorLeft
        backgroundColorRight
        fontColor
        audioAutoPlay
        audioUrl
        htmlTitle
        useBackgroundImage
        mini
        webSites{
          name
          url
        }
      }
    }
  }
}
</static-query>
<script>
import axios from 'axios'
import request from '../../utils/request.js'
export default {
  data() {
    return {
      show: false,
      handleClose: null,
      tokenForm: {
        token: "",
      },
    };
  },
  computed: {
    configs() {
      return this.$static.configs.edges[0].node;
    },
    user() {
      return localStorage.getItem("user");
    },
  },
  methods: {
      verifyToken: function (token) {
        return request({
            url: '/user?access_token=' + token
        })
    },
    Authentication(accessToken) {
      this.verifyToken(accessToken)
        .then((response) => {
          let result = response.data;
          let githubUsername = configs.githubUsername;
          if (githubUsername == result["login"]) {
              localStorage.setItem("Token",accessToken)
            Vue.prototype.$notify({
              title: "成功",
              message: "Token绑定成功",
              type: "success",
            });
          } else {
            Vue.prototype.$message({
              message: "Token用户不一致",
              type: "error",
            });
          }
        })
        .catch(() => {});
    },
    getUserInfo() {
      let token = localStorage.getItem("Token");
      let baseURL =
        "https://api.github.com/users/" +
        this.configs.githubUsername +
        "?access_token=" +
        token;
      axios.get(baseURL).then((data) => {
        let user = ({}.suer.avatarUrl = data.avatarUrl || null);
        this.location = data.location || null;
        this.blog = this.blog || null;
        this.followers = data.followers || 0;
        this.following = data.following || 0;
        localStorage.setItem("user", user);
      });
    },
    open(handleClose) {
      this.handleClose = handleClose;
      this.show = true;
    },
    onSubmit() {
      this.$refs["tokenForm"].validate((valid) => {
        if (valid) {
            this.Authentication(this.tokenForm.token);
          if (typeof this.handleClose == "function") {
            this.handleClose();
          }
        }
      });
      this.show = false;
    },
  },
};
</script>